<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="openAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column
                :index="getPage"
                type="index"
                label="序号"
                width="120"
              />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAddAssign(scope.row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="open(scope.row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件
                size-change：改变每页显示条数时触发
                current-change： 改变当前页数时触发
              -->
              <el-pagination
                :current-page="pForm.page"
                :page-sizes="[1, 2, 3, 4]"
                :page-size="pForm.pagesize"
                :total="total"
                layout="prev,pager,next, sizes"
                @size-change="hSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      title="新增角色"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            @click="hSubmit"
          >确定</el-button>
          <el-button size="small">取消</el-button>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
    </el-dialog>
    <!-- 编辑弹框 -->
    <el-dialog
      title="编辑角色"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            @click="hSubmit(roleForm)"
          >确定</el-button>
          <el-button size="small" @click="showDialog = false">取消</el-button>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
    </el-dialog>
    <!-- 分配权限dialog -->
    <el-dialog
      title="分配权限"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showAssignDialog"
    >
      <assign-tree ref="assign" :cur-id="curId" @success="hSuccess" />
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, delRoles, addRole, getRole, updateRole } from '@/api/setting'
import AssignTree from './AssignTree.vue'
export default {
  components: { AssignTree },
  data() {
    return {
      roleList: [],
      total: 0,
      pForm: {
        page: 1,
        pagesize: 2
      },
      // 控制dialog显示隐藏
      showDialog: false,
      showAssignDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      isEdit: false,
      curId: ''
    }
  },
  created() {
    // 初始化角色列表
    this.initRoles()
  },
  methods: {
    // 子组件成功保存 抛出事件
    hSuccess() {
      this.showAssignDialog = false
    },
    // 用户点击分配权限
    hAddAssign(id) {
      this.curId = id
      this.showAssignDialog = true
      this.$nextTick(() => {
        this.$refs.assign.getRoleDetail()
      })
    },
    // 根据id编辑角色
    async doEdit() {
      try {
        const res = await updateRole(this.roleForm)
        console.log(res)
        // 关闭dialog弹层
        this.showDialog = false
        this.initRoles()
      } catch (err) {
        console.log(err)
      }
    },
    // 打开新增dialog
    openAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    // 关闭dialog回调 清空表单
    hClose() {
      this.$refs.roleForm.resetFields()
    },
    // 新增角色
    async doAdd() {
      console.log(this.roleForm)
      try {
        await addRole(this.roleForm)
        // console.log(res)
        this.$message.success('新增角色成功')
        // 关闭dialog弹层
        this.showDialog = false
        // 重新请求角色列表信息
        this.initRoles()
      } catch (err) {
        console.log(err)
      }
    },
    // 确定新增 || 编辑
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return

        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 打开编辑dialog 表单回填
    async open(id) {
      this.showDialog = true
      this.isEdit = true
      // 打开编辑更新id
      this.roleId = id
      try {
        const res = await getRole(id)
        this.roleForm = res.data
      } catch (err) {
        console.log(err)
      }
    },
    // 自定义序列号
    getPage(index) {
      return index + 1 + (this.pForm.page - 1) * this.pForm.pagesize
    },
    // 删除
    async doDel(id) {
      try {
        const res = await delRoles(id)
        console.log(res)
        this.$message.success('删除成功')
        this.initRoles()
      } catch (err) {
        console.log(err)
      }
    },
    // 改变每页显示条数时触发
    hSizeChange(val) {
      console.log(val)
      this.pForm.pagesize = val
      this.pForm.page = 1
      this.initRoles()
    },
    // 改变当前页触发
    hCurrentChange(val) {
      this.pForm.page = val
      this.initRoles()
    },
    // 删除角色
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          if (this.roleList.length === 1 && this.pForm.page > 1) {
            this.pForm.page--
          }
          this.doDel(id)
        })
        .catch(() => {})
    },
    // 初始化角色列表
    async initRoles() {
      try {
        const res = await getRoles(this.pForm)
        // console.log(res)
        this.roleList = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
